package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.common.constants.WemediaConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.threadlocal.WmThreadLocalUtils;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmNewsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class WmNewsServiceImpl extends ServiceImpl<WmNewsMapper, WmNews> implements WmNewsService {


    /**
     * 自媒体文章发布
     *
     * @param wmNewsDto
     * @param isSubmit  是否为提交 1 为提交 0为草稿
     * @return
     */
    @Override
    @Transactional
    public ResponseResult saveNews(WmNewsDto wmNewsDto, Short isSubmit) {

        // 1 参数校验
        if (wmNewsDto == null || StringUtils.isBlank(wmNewsDto.getContent())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        // 2 保存或修改文章
        WmNews wmNews = new WmNews();
        // 将dto参数里面的值设置到wmNews
        BeanUtils.copyProperties(wmNewsDto, wmNews);
        //如果文章布局是自动，需要设置为null
        if (wmNewsDto.getType().equals(WemediaConstants.WM_NEWS_TYPE_AUTO)) {
            wmNews.setType(null);
        }
        // 处理dto参数 images封面集合 转换成 字符串
        String images = imageListToStr(wmNewsDto.getImages(), webSite);
        if (StringUtils.isNotBlank(images)) {

            wmNews.setImages(images);
        }
        saveWmNews(wmNews);


        // 2 保存或修改文章

        // 3 关联文章和素材关系表
        List<Map> contentList = JSON.parseArray(wmNewsDto.getContent(), Map.class);

        List<String> materials = parseContentImages(contentList);

        /**
         * 自媒体文章发布
         *
         * @param wmNewsDto
         * @param isSubmit 是否为提交 1 为提交 0为草稿
         * @return
         */
        if (isSubmit == WmNews.Status.SUBMIT.getCode() && materials.size() > 0) {
            // 【新增】私有方法
            saveRelativeInfoForContent(materials, wmNews.getId());
        }
        if (isSubmit == WmNews.Status.SUBMIT.getCode()) {
            saveRelativeInfoForCover(wmNewsDto, materials, wmNews);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据文章id查询文章
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult findWmNewsById(Integer id) {
        //1 参数检查
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //2 执行查询
        WmNews wmNews = getById(id);
        if (wmNews == null) {
           return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3 返回结果
        ResponseResult result = ResponseResult.okResult(wmNews);
        result.setHost(webSite);
        return result;
    }

    /**
     * 删除文章
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delNews(Integer id) {
        //1.检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"文章Id不可缺少");
        }
        //2.获取数据
        WmNews wmNews = getById(id);
        if(wmNews == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"文章不存在");
        }

        //3.判断当前文章的状态  status==9  enable == 1
        if(wmNews.getStatus().equals(WmNews.Status.PUBLISHED.getCode()) && wmNews.getEnable().equals(WemediaConstants.WM_NEWS_UPENABLE)){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"文章已发布，不能删除");
        }

        //4.去除素材与文章的关系
        wmNewsMaterialMapper.delete(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getNewsId,wmNews.getId()));

        //5.删除文章
        removeById(wmNews.getId());
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 上下架
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult downOrUp(WmNewsDto dto) {
        //1.检查参数
        if(dto == null || dto.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.查询文章
        WmNews wmNews = getById(dto.getId());
        if(wmNews == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"文章不存在");
        }

        //3.判断文章是否发布
        if(!wmNews.getStatus().equals(WmNews.Status.PUBLISHED.getCode())){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"当前文章不是发布状态，不能上下架");
        }

        //4.修改文章状态，同步到app端（后期做）TODO
        if(dto.getEnable() != null && dto.getEnable() > -1 && dto.getEnable() < 2){
            update(Wrappers.<WmNews>lambdaUpdate().eq(WmNews::getId,dto.getId()).set(WmNews::getEnable,dto.getEnable()));
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //保存文章封面和素材的关系
    private void saveRelativeInfoForCover(WmNewsDto wmNewsDto, List<String> materials, WmNews wmNews) {
        List<String> images = wmNewsDto.getImages();
        if (wmNewsDto.getType().equals(WemediaConstants.WM_NEWS_TYPE_AUTO)) {
            int size = materials.size();
            if (size > 0 && size <= 2) {
                images = materials.stream().limit(1).collect(Collectors.toList());
                wmNews.setType(WemediaConstants.WM_NEWS_SINGLE_IMAGE);
            } else if (size > 2) {
                images = materials.stream().limit(3).collect(Collectors.toList());
                wmNews.setType(WemediaConstants.WM_NEWS_MANY_IMAGE);
            } else {
                wmNews.setType(WemediaConstants.WM_NEWS_NONE_IMAGE);
            }
            if (images != null) {
                wmNews.setImages(imageListToStr(images, webSite));
            }
            updateById(wmNews);
        }
        if (images != null && images.size() > 0) {
            saveRelativeInfo(images,wmNews.getId(),WemediaConstants.WM_IMAGE_REFERENCE);
        }
    }
    private void saveRelativeInfoForImage(List<String> images, Integer newsId) {
        //***
        images = images.stream()
                .map(x->x.replace(webSite,"")
                        .replace(" ","")).collect(Collectors.toList());

        saveRelativeInfo(images, newsId, WemediaConstants.WM_IMAGE_REFERENCE);
    }


    //TODO 3.2 关联文章封面中的图片和素材关系  封面可能是选择自动或者是无图

    /**
     * @param materials
     * @param newsId
     */
    private void saveRelativeInfoForContent(List<String> materials, Integer newsId) {
        // 保存素材和文章关系，抽取一个独立方法执行关联操作，原因当前方法可以复用
        saveRelativeInfo(materials, newsId, WemediaConstants.WM_CONTENT_REFERENCE);
    }

    @Autowired
    WmMaterialMapper wmMaterialMapper;

    /**
     * @param materials 素材列表
     * @param newsId    文章ID
     * @param type      类型 0：内容中素材  1：封面素材
     */
    private void saveRelativeInfo(List<String> materials, Integer newsId, Short type) {
        //1 查询文章内容中的图片对应的素材ID
        List<WmMaterial> dbmaterials = wmMaterialMapper.selectList(Wrappers.<WmMaterial>lambdaQuery()
                .in(WmMaterial::getUrl, materials)
                .eq(WmMaterial::getUserId, WmThreadLocalUtils.getUser().getId())
        );
        //2 校验素材列表

        if (dbmaterials == null || dbmaterials.size() == 0) {
            throw new CustomException(AppHttpCodeEnum.FILE_MATERIAL_ISNULL_ERROR);
        }


        // {"1.jpg":55,"2.jpg":56}  草稿中的图片列表大于等于 数据库的素材
        Map<String, Integer> materialMap = dbmaterials.stream()
                .collect(Collectors.toMap(WmMaterial::getUrl, WmMaterial::getId));
        //将 db 转成流,然后 collect 聚合(将数据中的 URL 提出来作为 key Id ,作为值) 然后统一放在 materialmap 里面
        List<Integer> materialIds = new ArrayList<>();

        for (String url : materials) {
            Integer id = materialMap.get(url);
            if (id == null) {
                throw new CustomException(AppHttpCodeEnum.FILE_MATERIAL_ISNULL_ERROR);
            }
            materialIds.add(id);
        }

        //3 保存素材关系
        wmNewsMaterialMapper.saveRelations(materialIds, newsId, type);
    }


    private List<String> parseContentImages(List<Map> contentList) {

        List<String> materials = new ArrayList<>();
        for (Map<String, String> content : contentList) {
            if (content.get("type").equals(WemediaConstants.WM_NEWS_TYPE_IMAGE)) {
                String imageUrl = content.get("value")
                        .replace(webSite, ""); // ****
                materials.add(imageUrl);
            }
        }
        return materials;
    }


    @Autowired
    WmNewsMaterialMapper wmNewsMaterialMapper;

    /**
     * 【2】保存或修改文章
     *
     * @param wmNews 文章对象（前端传递）
     * @param
     */
    private void saveWmNews(WmNews wmNews) {

        wmNews.setCreatedTime(new Date());
        wmNews.setUserId(WmThreadLocalUtils.getUser().getId());
        wmNews.setSubmitedTime(new Date());
        wmNews.setEnable(WemediaConstants.WM_NEWS_UPENABLE); // 上架

        if (wmNews.getId() == null) { // 保存操作
            save(wmNews);
        } else {  // 修改
            // 当前文章 和 素材关系表数据删除
            wmNewsMaterialMapper.delete(
                    Wrappers.<WmNewsMaterial>lambdaQuery()
                            .eq(WmNewsMaterial::getNewsId, wmNews.getId()));

            updateById(wmNews);
        }
    }

    /**
     * 图片列表转字符串，并去除图片前缀
     *
     * @param images  图片列表
     * @param webSite 图片前缀
     */
    private String imageListToStr(List<String> images, String webSite) {
        if (images.size() > 0) {
            return images.toString()
                    .replace("[", "")
                    .replace("]", "")
                    .replace(webSite, "")
                    .replace(" ", "");

        }
        return null;
    }

    @Value("${file.oss.web-site}")
    private String webSite;

    /**
     * 查询所有自媒体文章
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findAll(WmNewsPageReqDto dto) {
        //1 参数检查
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam(); //分页

        //2 条件封装执行查询
        LambdaQueryWrapper<WmNews> query = new LambdaQueryWrapper<>();

        // 文章标题模糊查询
        if (StringUtils.isNotBlank(dto.getKeyword())) {
            query.like(WmNews::getTitle, dto.getKeyword());
        }
        // 频道id
        if (dto.getChannelId() != null) {
            query.eq(WmNews::getChannelId, dto.getChannelId());
        }
        // 文章状态
        if (dto.getStatus() != null) {
            query.eq(WmNews::getStatus, dto.getStatus());

        }
        // 发布时间
        if (dto.getBeginPubDate() != null && dto.getEndPubDate() != null) {
            query.between(WmNews::getPublishTime, dto.getBeginPubDate(), dto.getEndPubDate());

        }

        // 当前自媒体人文章
        WmUser user = WmThreadLocalUtils.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        query.eq(WmNews::getUserId, user.getId());
        // 按照创建日期倒序
        query.orderByDesc(WmNews::getCreatedTime);
        // 分页条件构建
        Page<WmNews> page = new Page<>(dto.getPage(), dto.getSize());

        //3 执行查询
        IPage<WmNews> pageResult = page(page, query);

        //4 返回封装查询结果
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) pageResult.getTotal(), pageResult.getRecords());
        // 处理文章图片 
        result.setHost(webSite);

        return result;
    }
}